package me.luger.project.fof.controller.admin;

import me.luger.core.commom.util.DateUtil;
import me.luger.core.dao.nutz.entity.page.DataTableRequest;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.core.mvc.interceptor.oplog.OpLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import me.luger.project.fof.service.FofFundIndicatorService;
import me.luger.project.fof.entity.FofFundIndicator;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

/**
 * 基金时序指标 controller
 */
@Controller
@RequestMapping("/private/fof/fofFundIndicator")
public class FofFundIndicatorController extends BaseController {

    private String viewBasePath = "/admin/modules/fofFundIndicator/";

    @Autowired
    private FofFundIndicatorService fofFundIndicatorService;

    @RequestMapping(value = "",name = "基金时序指标列表页面")
    public ModelAndView index(){
        return btlView(viewBasePath + "index");
    }

    @RequestMapping(value = "data",name = "获取基金时序指标列表")
    @ResponseBody
    public Object data(@RequestBody final DataTableRequest dataTableRequest){
        return fofFundIndicatorService.data(dataTableRequest);
    }



    @RequestMapping(value = "delete",name = "删除基金时序指标接口")
    @ResponseBody
    public Object delete(String[] ids){
        fofFundIndicatorService.delete(ids);
        return Result.success("delete success");
    }

    @RequestMapping(value = "/detail/{id}",name = "获取基金时序指标详细信息接口")
    public ModelAndView detail(@PathVariable("id") Integer id){
        FofFundIndicator fofFundIndicator = fofFundIndicatorService.fetchById(id);
        setAttr("fofFundIndicator",fofFundIndicator);
        return btlView(viewBasePath + "detail");
    }

    @RequestMapping(value = "add",name = "添加基金时序指标页面")
    public ModelAndView add(){
        return btlView(viewBasePath + "add");
    }

    @RequestMapping(value = "addDo",name = "添加基金时序指标接口")
    @ResponseBody
    @OpLog(name = "添加基金时序指标",msg = "添加基金时序指标")
    public Object adddo(FofFundIndicator fofFundIndicator){
        try{
            fofFundIndicator = fofFundIndicatorService.insert(fofFundIndicator);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/edit/{id}",name = "更新基金时序指标信息页面")
    public ModelAndView edit(@PathVariable Integer id){
        FofFundIndicator fofFundIndicator = fofFundIndicatorService.fetchById(id);
        setAttr("fofFundIndicator",fofFundIndicator);
        return btlView(viewBasePath + "edit");
    }

    @RequestMapping(value = "editDo",name = "更新基金时序指标接口")
    @ResponseBody
    public Object editDo(FofFundIndicator fofFundIndicator){
        try{
            fofFundIndicatorService.updateIgnoreNull(fofFundIndicator);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "init",name = "插入一些假的基金时序指标数据接口")
    @ResponseBody
    public Object init(){
        for(int j=1;j<=4;j++){
            FofFundIndicator fofFundIndicator = new FofFundIndicator();
            double lastUnitNav = 1.00;

            Date startDate = DateUtil.parseDate("2015-08-08",DateUtil.DATE_YYYY_MM_DD);
            for(int i=0;i<1000;i++){
                double accumulateUpDown = new Random().nextDouble()/10;

                if(new Random().nextDouble() >= 0.55){
                    accumulateUpDown = 0 - accumulateUpDown;
                }

                DecimalFormat df = new DecimalFormat("0.00");
                df.setRoundingMode(RoundingMode.HALF_UP);
                accumulateUpDown = Double.parseDouble(df.format(accumulateUpDown));

                lastUnitNav = lastUnitNav * (1 + accumulateUpDown);

                fofFundIndicator.setType(1);
                fofFundIndicator.setFid(j);
                fofFundIndicator.setUnitNav(lastUnitNav);
                fofFundIndicator.setYieldAnnually(format2(new Random().nextDouble() * 20));
                fofFundIndicator.setYield1m(format2(new Random().nextDouble() * 20));
                fofFundIndicator.setYield3m(format2(new Random().nextDouble() * 20));
                fofFundIndicator.setYield6m(format2(new Random().nextDouble() * 20));
                fofFundIndicator.setYield1y(format2(new Random().nextDouble() * 20));
                fofFundIndicator.setYield3y(format2(new Random().nextDouble() * 20));
                fofFundIndicator.setKumarRatio(format2(new Random().nextDouble()));
                fofFundIndicator.setSkewness(format2(new Random().nextDouble()));
                fofFundIndicator.setKurtosis(format2(new Random().nextDouble()));
                fofFundIndicator.setVolatilityAnnually(new Random().nextDouble() * 5);
                fofFundIndicator.setDrawdown(33D);
                fofFundIndicator.setWinningRatioMonthly(22D);
                fofFundIndicator.setNavDate(startDate);
                fofFundIndicator.setAccumulateNav(lastUnitNav);
                fofFundIndicator.setAccumulateUpDown(accumulateUpDown);
                Calendar c = DateUtil.toCalendar(startDate.getTime());
                c.add(Calendar.DAY_OF_YEAR,1);
                startDate = c.getTime();
                fofFundIndicatorService.fastInsert(fofFundIndicator);
            }
        }


        return Result.success();
    }

    public static Double format2(double value) {

        DecimalFormat df = new DecimalFormat("0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return Double.parseDouble(df.format(value));
    }
}
